from flaskapp.util.Sqlalchemy import db
from datetime import datetime

class Appointment(db.Model):
    __tablename__ = 'appointment'

    appointment_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    client_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False)
    house_id = db.Column(db.Integer, db.ForeignKey('house.house_id'), nullable=False)
    landlord_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False)
    appointment_time = db.Column(db.DateTime, nullable=False)
    status = db.Column(db.SmallInteger, nullable=False, default=0)  # 0-待处理，1-已接受，2-已拒绝，3-已取消
    created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)

    def to_dict(self):
        return {
            'appointment_id': self.appointment_id,
            'client_id': self.client_id,
            'house_id': self.house_id,
            'landlord_id': self.landlord_id,
            'appointment_time': self.appointment_time.strftime('%Y-%m-%d %H:%M:%S'),
            'status': self.status,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S'),
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S')
        }
